package com.hyb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyb.pojo.*;
import com.hyb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
public class IndexController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    InfoService infoService;
    @Autowired
    BigitemsService bigitemsService;
    @Autowired
    SmallitemsService smallitemsService;
    @RequestMapping({"/","/index1","/index1.html"})
    public String index(ModelMap maps){
        maps.addAttribute("m_id",1);
        List<Info> infoList = infoService.getAll(maps);
        List<Bigitems>  bigItemsList= bigitemsService.getAllBySort();
        List<Smallitems> smallItemsList = smallitemsService.getAll();
        maps.put("bigItemsList",bigItemsList);
        maps.put("infoList",infoList);
        maps.put("smallItemsList",smallItemsList);
        return "/index1";
    }
    @RequestMapping("/welcome")
    public String toWelcome(){
        return "/admin/welcome";
    }
    @RequestMapping("/admin-list")
    public String toAdminList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, ModelMap maps,Integer i_id,String username){
        PageHelper.startPage(pageNum,10);
        maps.addAttribute("i_id",i_id);
        maps.addAttribute("username",username);
        List<User> list1 =userService.getAll(maps);
        PageInfo<User> pageInfo =new PageInfo<>(list1);
        int count = userService.count();
        maps.addAttribute("count",count);
        maps.addAttribute("pageInfo",pageInfo);
        maps.addAttribute("list1",list1);
        return "/admin/admin-list";
    }
    @RequestMapping("/admin-add")
    public String toAdminAdd(ModelMap map){
        List<Role> roleList = roleService.getAll();
        map.addAttribute("roleList",roleList);
        return "/admin/admin-add";
    }

}
